package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-05
 */
@Api(description = "讲师管理")
@RestController//规定返回的是数据
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {
//把service注入
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;
    //1.查询讲师表所有的数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service的方法实现查询
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true)@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //分页功能的实现
    //current 当前页数
    //limit 每一页的记录数
    @ApiOperation("查询页数")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现
        teacherService.page(pageTeacher,null );
//        try {
//            int i=10/0;
//        }catch (Exception e){
//            //执行自定义异常
//            throw new GuliException(20001,"执行了自定义异常....");
//        }

        //求总记录数
        long total = pageTeacher.getTotal();
        //求每页的记录数据
        List<EduTeacher> records = pageTeacher.getRecords();
        //两种方法返回
        //方法一：
//        HashMap map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);
        //方法二：
        return R.ok().data("toatl",total).data("rows",records);

        //返回对应的数据
    }
    //条件查询分页的方法
    //@requestbody 该注解会自动将前端数据传入其中，自动封装（必须要求以post的方式提交）
    //@responbody 该注解会将将数据以json的形式上传
    //@RequestBody(required = false)该条件值可以没有，就是为空也可以运行
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建分页的对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //创建条件的对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件的查询
        //动态sql，先指明查询的条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件是否为空，如果不是为空，那就添加条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            //使用的字段名不是属性名字，而是字段名
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法的执行
        teacherService.page(pageTeacher,wrapper);
        //求总记录数
        long total = pageTeacher.getTotal();
        //求每页的记录数据
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("toatl",total).data("rows",records);
    }
    //添加讲师接口的方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //根据id查询讲师
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    //讲师修改功能
    //原本应该用put注解，但是只有用post注解才能用@request注解传输数据
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

